package com.luck.easyfoodbatch.mvp.home;

import android.content.Context;
import android.support.annotation.Nullable;

import com.luck.easyfoodbatch.callback.JsonCallback;
import com.luck.easyfoodbatch.entity.Categorys;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.cache.CacheMode;
import com.lzy.okhttputils.model.HttpParams;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/5/9.
 */

public class CategoryListPresenter implements CategoryListModel {
    private final String TAG=getClass().getSimpleName();
    private CategoryListView mCategoryListView;
    private Context mContext;

    public CategoryListPresenter(CategoryListView categoryListView, Context context) {
        this.mCategoryListView = categoryListView;
        this.mContext = context;
    }

    @Override
    public void getCategoryList() {
        HttpParams params = new HttpParams();
        OkHttpUtils.post(HttpUtil.CATEGORY_LIST)
                .tag(this)
                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
                .params(params)
                .execute(new JsonCallback(mContext, Categorys.class) {
                    @Override
                    public void onResponse(boolean isFromCache, Object data, Request request, @Nullable Response response) {
                        mCategoryListView.getCategoryListsSuccess((Categorys) data);
                        mCategoryListView.hideLoading();
                    }

                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                        mCategoryListView.hideLoading();
                        mCategoryListView.httpStatusError();
                    }
                });

    }
}
